package md.lanjing.com.module_deal_center.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.TimeUtils;
import com.lxh.baselibray.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

import md.lanjing.com.module_deal_center.R;
import md.lanjing.com.module_deal_center.R2;
import md.lanjing.com.module_deal_center.bean.PayMoneyRespoce;
import com.lxh.baselibray.constant.Constant;
import md.lanjing.com.module_deal_center.contact.PayMoneyContact;
import retrofit2.Response;

/**
 * 付款-选择支付方式
 */
public class PayMoneyActivity extends MvpActivity<PayMoneyContact.PayMoneyPresent> implements PayMoneyContact.IPayMoneyView {
    public static String TRANSFER_ID = "transfer_id";
    @BindView(R2.id.tv_order_number)
    TextView tvOrderNumber;
    @BindView(R2.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R2.id.tv_buy_id)
    TextView tvBuyId;
    @BindView(R2.id.tv_count)
    TextView tvCount;
    @BindView(R2.id.tv_pay_buy_total_price)
    TextView tvPayBuyTotalPrice;
    @BindView(R2.id.rg)
    RadioGroup rg;
    @BindView(R2.id.rb_ali)
    RadioButton rbAli;
    @BindView(R2.id.rb_weixin)
    RadioButton rbWeixin;
    @BindView(R2.id.rb_card)
    RadioButton rbCard;

    int payType = 0;//0-支付宝 1-微信 2-银行卡
    String transferId;

    PayMoneyRespoce.DataBean dataBean;
    @Override
    public void initData(Bundle savedInstanceState) {
        rg.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb_ali) {
                payType = 0;

            } else if (checkedId == R.id.rb_weixin) {
                payType = 1;

            } else if (checkedId == R.id.rb_card) {
                payType = 2;

            }
        });
        rg.getChildAt(0).performClick();

        transferId = getIntent().getStringExtra(TRANSFER_ID);
        mPresent.queryPayMoneyMsg(context,transferId);


    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_pay_money;
    }


    @OnClick(R2.id.btn_confirm)
    public void onViewClicked() {
        Intent intent = new Intent(this, PayMoneyDetailActivity.class);
        intent.putExtra(PayMoneyDetailActivity.PAY_TYPE, payType);
        intent.putExtra(PayMoneyDetailActivity.TRANSFER_ID, transferId);
        intent.putExtra(PayMoneyDetailActivity.BILL_ID,dataBean!=null?dataBean.getBillId():"");
        startActivity(intent);
    }


    @Override
    public void getPayMoneyMsgData(Response<PayMoneyRespoce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            dataBean = response.body().getData();
            if (ObjectUtils.isEmpty(dataBean))return;
            tvOrderNumber.setText("订单号：" + dataBean.getBillId());
            tvOrderTime.setText("订单时间：" + TimeUtils.millis2String(dataBean.getLockTime()));
            tvCount.setText("购买数量：" + dataBean.getTokenNum());
            tvBuyId.setText("购买人ID:" + dataBean.getBuyerId());
            tvPayBuyTotalPrice.setText("购买总价：" + dataBean.getCnyPrice() * dataBean.getTokenNum() + "CNY");
            if (dataBean.getAliPay()==1){
                rbAli.setVisibility(View.VISIBLE);
            }else {
                rbAli.setVisibility(View.GONE);
            }
            if (dataBean.getWeChat()==1){
                rbWeixin.setVisibility(View.VISIBLE);
            }else {
                rbWeixin.setVisibility(View.GONE);
            }
            if (dataBean.getBank()==1){
                rbCard.setVisibility(View.VISIBLE);
            }else {
                rbCard.setVisibility(View.GONE);
            }
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected PayMoneyContact.PayMoneyPresent createPresent() {
        return new PayMoneyContact.PayMoneyPresent();
    }
}
